function saveImageToPhotosAlbum(option) {
  return new Promise((resolve, reject) => {
    wx.saveImageToPhotosAlbum({
      ...option,
      success: resolve,
      fail: reject,
    })
  })
}
var app = getApp()
var wxParse = require("../../../../utils/wxParse.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hs_l_key: false,
    html: '',
    maskey: true,
    maskbtn_key: true,
    listall: [],
    img: '',
    jifen: false,
  },
  closeZhe: function () {
    this.setData({
      jifen: false
    })
  },
  // 取消遮罩层
  mask_key: function () {
    this.setData({
      maskey: true
    })
  },
  // 出现遮罩层
  hasmask: function (e) {
    var that = this
    var id = that.data.id
    var zid = that.data.zid
    app.util.request({
      'url': 'entry/wxapp/zixun.generate',
      data: {
        id: id,
        zid: zid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          img: res.data
        })
        // console.log(that.data.img)
      },
      fail: function (err) {
        console.log(err)
      },
    })
    this.setData({
      maskey: false
    })
  },

  goBack: function () {
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/zixunList/zixunList',
    })
  },

  // 下载图片
  maskbtn: function () {
    // 保存到相册
    let that = this
    const imageFile = that.data.img
    wx.showLoading({
      title: '图片保存中...'
    })
    wx.downloadFile({
      url: imageFile,
      success: function (res) {
        // 图片保存到本地
        console.log(res.tempFilePath)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.hideLoading()
            wx.showModal({
              title: '图片已存入相册',
              content: '好东西要分享，发给朋友们看看。',
              showCancel: false
            })
          },
          fail: function (err) {
            console.log(err)
            if (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg === 'saveImageToPhotosAlbum:fail auth deny' || err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
              console.log('22222222222222222222222222222222=============================')
              // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                showCancel: false,
                success: modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log('settingdata', settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限成功,再次点击按钮即可保存',
                          showCancel: false
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限失败，将无法保存到相册！',
                          showCancel: false
                        })
                      }
                    },
                    fail(failData) {
                      console.log('failData', failData)
                    },
                    complete(finishData) {
                      console.log('finishData', finishData)
                    }
                  })
                }
              })
            }
          },
          complete(res) {
            wx.hideLoading()
          }
        })
      }
    })
  },

  imageErrorAuth() {
    // 授权失败 提示授权操作
    wx.showModal({
      title: '提示',
      content: '需要您授权保存至相册',
      showCancel: false,
      success: modalSuccess => {
        wx.openSetting({
          success(settingData) {
            console.log("settingData", settingData)
            if (settingData.authSetting['scope.writePhotosAlbum']) {
              wx.showModal({
                title: '提示',
                content: '获取权限成功,再次保存图片即可',
                showCancel: false
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '获取权限失败，将无法保存到相册',
                showCancel: false
              })
            }
          },
          fail(failData) {
            console.log("failData", failData)
          },
          complete(finishData) {
            console.log("finishData", finishData)
          }
        })
      }
    })
  },

  // 点赞
  diangood: function (e) {
    var that = this
    var dz = parseInt(that.data.dz)
    var openid = wx.getStorageSync('openid')
    app.util.request({
      'url': 'entry/wxapp/zixun.adddz',
      data: {
        id: that.data.id,
        openid: openid,
        type: 1
      },
      success: function (res) {
        console.log(res)
      },
      fail: function (err) {
        console.log(err)
      },
    })
    that.setData({
      hs_l_key: !this.data.hs_l_key,
      dz: (dz + 1)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var bgc = wx.getStorageSync('color')
    var id = options.id
    var zid = options.zid
    var orderid = options.orderid
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    wx.loadFontFace({
      family: 'SourceHanSansCN-Medium', //定义字体名称
      source: 'url("https://www.baiyiyl.com/addons/hyb_yl/SourceHanSansCN-Regular.ttf")', //字体资源地址
      success(res) {
        console.log(res.status) //加载字体结果
      },
    });
    that.setData({
      id: id,
      zid:zid,
      orderid:orderid
    })


    that.getDetail()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    // if(that.data.jifen){
    //   setTimeout(function(){
    //     that.setData({
    //       jifen:false
    //     })
    //   },2000)
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getDetail() {
    var that = this
    var zid = that.data.zid
    var openid = wx.getStorageSync('openid')
    var orderid = that.data.orderid
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.cfdetail',
      data: {
        id: that.data.id,
        zid: zid,
        openid: openid,
        orderid:orderid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          detail:res.data
        })
        wxParse.wxParse('scontent', 'html', res.data.content, that, 5)

      },
      fail: function (err) {
        console.log(err)
      },
    })

  },
})